<template>
  <div class="excel-to-json app-container">
    <ContentLayout :title="'Excel转Json'">
      <div>
        <CustomUpload v-model:fileList="fileList" :limit="2"></CustomUpload>
        <el-button type="primary" @click="handleContrastKey">对比key</el-button>
        <el-button type="primary" @click="handleContrastValue"
          >对比value</el-button
        >
        <el-button type="primary" @click="handleContrastKeyAndValue"
          >对比key与value</el-button
        >
        <el-button type="danger" @click="handleClear"> 清空</el-button>
      </div>
    </ContentLayout>
    <ShowJsonDialog
      v-model="showJsonDialog"
      :json1="diffArr1"
      :json2="diffArr2"
    ></ShowJsonDialog>
  </div>
</template>

<script setup lang="ts">
import type { UploadUserFile } from "element-plus";
import { ElMessageBox } from "element-plus";
import { ref } from "vue";
import { getJsonDataByFile } from "@/utils/excelToJson";
const fileList = ref<UploadUserFile[]>([]);
const showJsonDialog = ref(false);
let diffArr1 = ref<any>({});
let diffArr2 = ref<any>({});
function handleClear() {
  ElMessageBox.confirm("确定要清空吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    fileList.value = [];
  });
}
// 解析json文件

async function handleContrastKey() {
  const func = (k: string, key: string, v: string, value: string) => {
    return k == key;
  };
  contrast(func);
}
async function handleContrastValue() {
  const func = (k: string, key: string, v: string, value: string) => {
    return v == value;
  };
  contrast(func);
}
async function handleContrastKeyAndValue() {
  const func = (k: string, key: string, v: string, value: string) => {
    return k == key && v == value;
  };
  contrast(func);
}
async function contrast(func: Function) {
  const file1 = (await getJsonDataByFile(fileList)) as Record<string, any>;
  const file2 = (await getJsonDataByFile(fileList, 1)) as Record<string, any>;
  let entriesArr1 = Object.entries(file1);
  let entriesArr2 = Object.entries(file2);
  //   采集不一致的key与value

  entriesArr1.forEach(([key, value]) => {
    if (!entriesArr2.some(([k, v]) => func(k, key, v, value))) {
      // diffArr1.value[key] = value;
    }
  });
  const items = [
    "all",
    "integral",
    "income",
    "cost",
    "consumePoint",
    "rechargeGamePoint",
    "giftBagPurchase",
    "integralIncome",
    "sales",
    "discount",
    "peopleToBuy",
    "buySuccess",
    "buyFailed",
    "fromBuy",
    "fromCost",
    "fromBind",
    "langChange",
    "roleChange",
    "logChange",
    "ulChinese",
    "copySignError",
    "copyError",
    "sureChangeAgain",
    "payAccount",
    "howToGetLogCode",
    "logRoleSelect",
    "logNewRole",
    "popTips1",
    "popTips2",
    "logError",
    "thxForBuy",
    "checkOrder",
    "noGoods",
    "nullGoods",
    "goodsList",
    "maxError",
    "signOOT",
    "pcError",
    "checkLogCode",
    "orderStatus",
    "backMain",
  ];
  entriesArr2.forEach(([key, value]) => {
    if (!entriesArr1.some(([k, v]) => func(k, key, v, value))) {
      if (!items.includes(key)) {
        diffArr2.value[key] = value;
        diffArr1.value[key] = file1[key];
      }
    }
  });
  showJsonDialog.value = true;
}
</script>

<style lang="scss" scoped></style>
